
package management;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Cucnth
 */
public class GUIInteraction {
    public static ResultSet rs = null;

//Read to table
    public static void readToTable1(String sql, JTable tableName) throws SQLException {
        DefaultTableModel dm = (DefaultTableModel) tableName.getModel();
        for (int i = dm.getRowCount() - 1; i >= 0; i--) {
            dm.removeRow(i);
        }
        try {
            ResultSet rs = MyConnection.getResultSet(sql);
            ResultSetMetaData rm = rs.getMetaData();
            while (rs.next()) {
                Vector row = new Vector();
                for (int i = 1; i <= rm.getColumnCount(); i++) {
                    row.add(rs.getString(i));
                }
                dm.addRow(row);
            }
            tableName.setModel(dm);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
     public static void readToTable(String sql, JTable tableName) throws SQLException {
        DefaultTableModel dm = (DefaultTableModel) tableName.getModel();
        for (int i = dm.getRowCount() - 1; i >= 0; i--) {
            dm.removeRow(i);
        }
        Vector cols = new Vector();
        try {
            ResultSet rs = MyConnection.getResultSet(sql);
            ResultSetMetaData rm = rs.getMetaData();
            for (int i = 1; i <= rm.getColumnCount(); i++) {
                cols.add(rm.getColumnName(i));
            }
            dm.setColumnIdentifiers(cols);
            while (rs.next()) {
                Vector row = new Vector();
                for (int i = 1; i <= rm.getColumnCount(); i++) {
                    row.add(rs.getString(i));
                }
                dm.addRow(row);
            }
            tableName.setModel(dm);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    
}
